﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CYTS.Aoyou.Framework.Cache;
using CYTS.Aoyou.Framework.CAInterface;
using CYTS.Aoyou.Framework.DAInterface;
using CYTS.Aoyou.Framework.Database;

namespace CYTS.Aoyou.Framework.Performance.WCFPerformance
{
    /// <summary>
    /// 
    /// </summary>
    public class WCFPerformanceHelper
    {
        private DateTime StartTime;
        private DateTime EndTime;
        private string dataBaseName = "AoYouPerformance";
        public WCFPerformanceHelper(DateTime dt)
        {
            StartTime = dt;
        }

        /// <summary>
        /// 每次都记录rades值，当超过接口时限，则存入monogdb；当储存rades数据5分钟，则求平均值存入monogdb
        /// </summary>
        public void SetData(WCFPerformanceEntity wcfEntity)
        {
            wcfEntity.ExecuteTime = functionTime();
            IPerformanceCacheAdapter perAdapter = CacheAdapterFactory.GetInstance().GetCacheAdapter<IPerformanceCacheAdapter>();
            IPerformanceDataAdapter perdataAdapter = DataAdapterFactory.GetInstance().GetDataAdapter<IPerformanceDataAdapter>();
            var oldEntity = GetData(wcfEntity.InterFaceName + "/" + wcfEntity.ExecuteFunName);//get redis
            if (wcfEntity.ExecuteTime > 8000)//报警，直接记录mongodb，不累加radis
            {
                wcfEntity.RecordTime = DateTime.Now;
                wcfEntity.AvgExcureTime = wcfEntity.ExecuteTime;
                perdataAdapter.Set<WCFPerformanceEntity>(dataBaseName, "WCFPerformance" + DateTime.Now.ToString("yyyyMMdd"), wcfEntity);
                if (oldEntity == null)
                {
                    wcfEntity.LastRecordTime = DateTime.Now;
                }
            }
            else//记录radis
            {
                if (oldEntity != null)
                {
                    wcfEntity.TotelRecord = oldEntity.TotelRecord + 1;
                    wcfEntity.TotelExcureTime = oldEntity.TotelExcureTime + wcfEntity.ExecuteTime;
                    DateTime dt = DateTime.Now;
                    if ((dt - oldEntity.LastRecordTime).TotalSeconds > 600)//判断时间，记录monogdb，并清空当前radis的累积数据
                    {
                        //调用monogdb方法,算平均时间
                        wcfEntity.AvgExcureTime = wcfEntity.TotelExcureTime / wcfEntity.TotelRecord;
                        wcfEntity.RecordTime = DateTime.Now;
                        perdataAdapter.Set<WCFPerformanceEntity>(dataBaseName, "WCFPerformance" + DateTime.Now.ToString("yyyyMMdd"), wcfEntity);
                        //清空累加值
                        wcfEntity.TotelExcureTime = 0;
                        wcfEntity.TotelRecord = 0;
                        wcfEntity.LastRecordTime = DateTime.Now;
                    }
                    else
                    {
                        wcfEntity.LastRecordTime = oldEntity.LastRecordTime;
                    }
                }
                else //第一次记录radis
                {
                    wcfEntity.TotelRecord = 1;
                    wcfEntity.TotelExcureTime = wcfEntity.ExecuteTime;
                    wcfEntity.LastRecordTime = DateTime.Now;
                }
            }
            perAdapter.Set<WCFPerformanceEntity>(wcfEntity.InterFaceName + "/" + wcfEntity.ExecuteFunName, wcfEntity);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public WCFPerformanceEntity GetData(string key)
        {
            IPerformanceCacheAdapter perAdapter = CacheAdapterFactory.GetInstance().GetCacheAdapter<IPerformanceCacheAdapter>();
            try
            {
                var oldEntity = perAdapter.Get<WCFPerformanceEntity>(key);//get redis
                return oldEntity;
            }
            catch
            {
                return null;
            }
        }


        private double functionTime()
        {
            EndTime = DateTime.Now;
            TimeSpan ts = EndTime - StartTime;
            return ts.TotalMilliseconds;
        }
    }
}
